package com.yanqu.road.server.gameplayer.module.cricket.cmd;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.cricket.CricketData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.cricket.CricketMgr;
import com.yanqu.road.server.gameplayer.module.cricket.CricketModule;
import com.yanqu.road.server.gameplayer.module.cricket.battle.CricketBattle;
import com.yanqu.road.server.gameplayer.module.cricket.entity.DbUserCricketBattleLog;
import com.yanqu.road.server.gameplayer.module.cricket.model.CricketBattleData;
import com.yanqu.road.server.gameplayer.module.cricket.model.CricketGameResult;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.List;


@Cmd(code = GameProtocol.S_CRICKET_RED_DOT, desc = "蛐蛐红点")
public class CricketRedDotCmd extends AbstractCommand {

    private CricketProto.CricketRedDotReqMsg reqMsg;

    private CricketProto.CricketRedDotRespMsg.Builder respMsgBuilder;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        this.reqMsg = CricketProto.CricketRedDotReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        this.respMsgBuilder = CricketProto.CricketRedDotRespMsg.newBuilder();
        this.respMsgBuilder.setBattleLog(false);
       if (this.reqMsg.getLastBattleLogTimestamp() >= 0) {
           List<DbUserCricketBattleLog> cricketBattleLogList = player.getModule(CricketModule.class).getCricketBattleLogList();
           for (DbUserCricketBattleLog dbUserCricketBattleLog : cricketBattleLogList) {
               if (dbUserCricketBattleLog.getBattleType() != 1) {
                   continue;
               }
               if (dbUserCricketBattleLog.getCreateTime() > this.reqMsg.getLastBattleLogTimestamp()) {
                   this.respMsgBuilder.setBattleLog(true);
                   break;
               }
           }
       }
    }

    @Override
    public void sendData(GamePlayer player) {
        player.sendPacket(ClientProtocol.U_CRICKET_RED_DOT, respMsgBuilder);
    }
}
